package com.example.afinal;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.ViewPager;

import com.example.afinal.Plus.MoreWindow;
import com.example.afinal.bean.User;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    MoreWindow mMoreWindow;
    private ViewPager vpMain;
    private BottomNavigationView bnvMain;
    private ConstraintLayout idContainer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},123);
        }

        //状态栏透明，设置新建按钮
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        idContainer = (ConstraintLayout) findViewById(R.id.id_container);
        mMoreWindow = new MoreWindow(this);
        mMoreWindow.init(idContainer);


        vpMain = findViewById(R.id.vp_main);
        bnvMain = findViewById(R.id.bnv_main);
        MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager());
        vpMain.setOffscreenPageLimit(4);
        vpMain.setAdapter(adapter);
        bnvMain.setItemTextAppearanceActive(R.style.bottom_selected_text);  //设置选中的字体的样式

        bnvMain.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.bnv_1:
                        vpMain.setCurrentItem(0);
                        break;
                   /* case R.id.bnv_2:
                        vpMain.setCurrentItem(1);
                        break;*/
                    case R.id.bnv_3:
                        showMoreWindow();
                        break;
                    case R.id.bnv_4:
                        vpMain.setCurrentItem(2);
                        break;
                    case R.id.bnv_user:
                        vpMain.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });

        Intent intert = getIntent();
        User.name=intert.getStringExtra("name");
        User.pwd=intert.getStringExtra("pwd");
        int id = intert.getIntExtra("sta", -1);
        if (id > 0) {
            if (id == 1) {
                vpMain.setCurrentItem(2);
            } else if (id == 2) {
                vpMain.setCurrentItem(0);
            }else if(id==3){
                vpMain.setCurrentItem(3);
            }
        }
    }

    /**
     * 新建跳出窗口
     */
    private void showMoreWindow() {
        if (null == mMoreWindow) {
            mMoreWindow = new MoreWindow(this);
            mMoreWindow.init(idContainer);
        }

        mMoreWindow.showMoreWindow(idContainer);
    }
}
